#include "main_window.h"

namespace
{

inline QToolButton* create_styled_toolbutton()
{
    QToolButton *button = new QToolButton();

    button->setStyleSheet("QToolButton{"
                          "    background-color: rgb(255, 255, 255);"
                          "    border: 1px solid #000000;"
                          "}"
                          "QToolButton:checked{"
                          "    background-color: rgb(64, 64, 64);"
                          "    color: #FFFFFF;"
                          "}");

    button->setMinimumHeight(30);
    button->setMinimumWidth(109);

    QFont font = button->font();
    font.setPixelSize(18);
    button->setFont(font);

    return button;
}

} // end of anonymous namespace

main_window::main_window(QWidget *parent)
    : QMainWindow (parent)
    , taskbar_    (new taskbar(this, false))
    , full_screen_(false)
{
    taskbar_->add_button_named("test1" , true);
    taskbar_->add_button_named("test2" , true);
    taskbar_->add_button_named("test3" , true);
    setMouseTracking(true);

    connect(taskbar_, SIGNAL(size_changed()), this, SLOT(move_taskbar()));
}

void main_window::add_widget(QWidget *widget)
{
//    widget->setWindowFlags(Qt::Tool | Qt::MSWindowsFixedSizeDialogHint | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    widget->setWindowFlags(widget->windowFlags() | Qt::Tool); // | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
//    widget->setParent(this);
    widget->show();

    childs_.insert(widget);
}

void main_window::set_fullscreen(bool full)
{
    if(full_screen_ != full)
    {
        full_screen_ = full;

        if (full)
        {
            setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); // | Qt::X11BypassWindowManagerHint);
            showFullScreen();
        }
        else
        {
            setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
            showNormal();
        }
    }
}

void main_window::changeEvent(QEvent *e)
{
    if(full_screen_ && e->type() == QEvent::WindowStateChange)
    {
        if (isMinimized())
        {
            showFullScreen();
            return e->ignore();
        }
    }

    return QMainWindow::changeEvent(e);
}

void main_window::resizeEvent(QResizeEvent *e)
{
    move_taskbar();
    QMainWindow::resizeEvent(e);
}

void main_window::moveEvent(QMoveEvent *e)
{
    move_taskbar();
    QMainWindow::moveEvent(e);
}

void main_window::move_taskbar()
{
    if (taskbar_)
    {
        const auto &rect = geometry();
        const auto p = QPoint(rect.x() + (rect.width()-taskbar_->width())/2, rect.y() + rect.height() - taskbar::height);
        taskbar_->move(p);
    }
}

void main_window::mouseMoveEvent(QMouseEvent *e)
{
    if (taskbar_ && e->buttons() == Qt::NoButton)
    {
        const auto &rect = geometry();
        const auto &bottom_rect = rect.adjusted(0, rect.height() - taskbar::height, 0, 0);
        const auto pos = mapToGlobal(e->pos());

        if (bottom_rect.contains(pos))
        {
            if (!taskbar_->isVisible())
                taskbar_->show();
        }
        else
        {
            if (taskbar_->isVisible())
                taskbar_->hide();
        }
    }

    QMainWindow::mouseMoveEvent(e);
}


